﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Models
{
    public partial class ProjectDbContext:DbContext
    {
        public ProjectDbContext(DbContextOptions options)
           : base(options)
        {

        }
        #region 多对多关系表
        public DbSet<UserBegoodatRelation> UserBegoodatRelation { get; set; }  // 雇员擅长关系
        public DbSet<UserDesignRelation> UserDesignRelation { get; set; }  // 用户设计工具
        #endregion




        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //设置不跟踪所有查询  
            optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

            base.OnConfiguring(optionsBuilder);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 用户手机号唯一约束
            modelBuilder.Entity<WebUser>()
            .HasIndex(u=>u.Phone)
            .IsUnique();

            //base.OnModelCreating(modelBuilder);
            //雇员--擅长 关系配置
            modelBuilder.Entity<UserBegoodatRelation>().HasKey(r => new { r.UserId, r.BeGoodAtId });
            modelBuilder.Entity<UserBegoodatRelation>()
                .HasOne(r => r.User)
                .WithMany(x => x.UserBegoodatRelation)
                .HasForeignKey(r => r.UserId);
            modelBuilder.Entity<UserBegoodatRelation>()
                .HasOne(r => r.BeGoodAt).WithMany(b => b.EmployeeBegoodatRelation)
                .HasForeignKey(r => r.BeGoodAtId);
            // 用户设计工具多对多关系
            modelBuilder.Entity<UserDesignRelation>().HasKey(r => new { r.UserId, r.DesignToolId });
            modelBuilder.Entity<UserDesignRelation>()
                .HasOne(r => r.User)
                .WithMany(u => u.UserDesignRelations)
                .HasForeignKey(r => r.UserId);
            modelBuilder.Entity<UserDesignRelation>()
                .HasOne(r => r.DesignTool).WithMany(d => d.UserDesignRelations)
                .HasForeignKey(r => r.DesignToolId);

           
        }
    }
}
